<?php
/**
 * The base controller which is used by the Front and the Admin controllers
 */

use EasyWeChat\Foundation\Application;

class Base_Controller extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
        $this->load->database();
		$web_category=$this->web_category=explode('|', $this->config->item('web_category'));
		// var_dump($this->web_category);exit;
		$web_category_txt=$this->web_category_txt=explode('|', $this->config->item('web_category_txt'));
	}

    
}
class FOX_Controller extends Base_Controller
{
    public $USER_KEY;
    public $OPENID;
	public $USER_LAT;
	public $USER_LNG;
    private $_sessionUserKey = 'wechat_user';
	function __construct(){
		parent::__construct();	
		$data='';
		//判断安装
		$file=FCPATH.'install.lock';
		if (!is_file($file)){
			redirect(site_url('install'));
		}
		$this->load->database();

		if($this->config->item('site_close')=='off'){
			echo '网站维护关闭中';
			exit;
		}
        //pp(get_cookie('admin_key'));exit();
		$machine_key = $this->getLogCookie(); // 用户唯一key,FOX_Controller全局使用
		$data['user'] = array();
		$data['messages_num']=0;
		$loveme='';
		if($this->session->userdata('user_id')){
			//取一个用户信息
			$data['user']=$this->user_m->get_user_by_user_id($this->session->userdata('user_id'));
			$data['user']['uname']=get_user_name($data['user']['user_name'],$data['user']['user_uname']);
			//获取头像
			$this->load->model('upload_m');
			$data['user']['big_avatar']=$this->upload_m->get_avatar_url($this->session->userdata('user_id'), 'big');
			$data['user']['middle_avatar']=$this->upload_m->get_avatar_url($this->session->userdata('user_id'), 'middle');
			$data['user']['small_avatar']=$this->upload_m->get_avatar_url($this->session->userdata('user_id'), 'small');
            //推广COOKIES
            $user_referee = get_cookie('user_referee',TRUE);
            if(isset($user_referee) && $data['user']['user_referee']==0){
				if($user_referee != $this->session->userdata('user_id')){
					$this->user_m->update_user_referee($this->session->userdata('user_id'),$user_referee);
				}
            }
		}else{
			$loveme=get_cookie('loveme',TRUE);
			$salt =get_salt();
			if(isset($loveme)){
				if($this->user_m->checklogin($loveme,$salt)){
					redirect(site_url());
				}
			}
			$this->USER_KEY = $machine_key;
		}
		if(isMobile()){
            $this->load->set_front_theme('wap');
            $data['viewmulu']=base_url('public/wap/');
        }else{
            $this->load->set_front_theme('wap');
            $data['viewmulu']=base_url('public/wap/');

        }
		$data['foot_memu'] = 'home';
		$this->load->vars($data);
	}

    protected function getLogCookie() {
        $this->load->helper('cookie');
        if(get_cookie('user_key')){
            $cookie = null;
            $cookie = get_cookie('user_key');
        }else{
            $cookie = null;
            $name = 'machine_key';
            $cookieStr = get_cookie($name);
            if (empty($cookieStr)) {
                $cookie = uniqid().mt_rand(1,1000000);
                set_cookie($name, serialize(array('id'=>$cookie)), 60*60*24*365); //一年的cookie
            } else {
                $cookieArr = unserialize($cookieStr);
                $cookie = $cookieArr['id'];
            }
        }

        return $cookie;
    }

}

class Admin_Controller extends Base_Controller 
{
	function __construct()
	{
		parent::__construct();
		//载入模板
		$this->load->set_front_theme('manage');
		//判断安装
		$file=FCPATH.'install.lock';
		if (!is_file($file)){
			redirect(site_url('install'));
		}
		// 判断登录
		if(!$this->auth->is_login())
		{
			redirect(site_url('user/login'));
		}
		// 链接数据库
		$this->load->database();
		$loveme='';
		if($this->session->userdata('user_id')){
			//取一个用户信息
			$data['user']=$this->user_m->get_user_by_user_id($this->session->userdata('user_id'));
			$data['user']['uname']=get_user_name($data['user']['user_name'],$data['user']['user_uname']);
			$data['user']['user_group_name']=$this->user_m->get_user_by_user_group($data['user']['user_group']);
			//获取头像
			$this->load->model('upload_m');
			$data['user']['big_avatar']=$this->upload_m->get_avatar_url($this->session->userdata('user_id'), 'big');
			$data['user']['middle_avatar']=$this->upload_m->get_avatar_url($this->session->userdata('user_id'), 'middle');
			$data['user']['small_avatar']=$this->upload_m->get_avatar_url($this->session->userdata('user_id'), 'small');
		}else{
			$loveme=get_cookie('loveme',TRUE);
			$salt =get_salt();
			if(isset($loveme)){
				if($this->user_m->checklogin($loveme,$salt)){
					redirect(site_url());
				}
			}
		}
		$uid = $this->session->userdata('user_id');
        $con = $this->router->fetch_class(); // 当前控制器
        $func = $this->router->fetch_method(); // 当前方法名
        // 获取当前菜单
        $current = $this->authRule_m->get_current($con,$func);
        $data['current']=$current[0];

        $userinfo = $this->user_m->get_user_with_group($uid);
        $menu_access_ids = $userinfo['rules'];
		// 检查访问权限
		$allow_controller_name = array('Upload');//放行控制器名称
        $allow_action_name = array();//放行函数名称
        if ($userinfo['group_id'] != 1 && !$this->auth->check($con . '/' . $func,
                $uid) && !in_array($con, $allow_controller_name) && !in_array($func,
                $allow_action_name)
        ) {
            show_message('没有访问权限',site_url('user/logout'),1);
        }
		// 获取登录用户的权限菜单
        if ($userinfo['group_id'] != 1) {
            $menu_where = "AND id in ($menu_access_ids)";
        } else {
            $menu_where = '';
        }
        $menu = $this->authRule_m->get_menu_by_auth($menu_where);
        $menu = $this->getMenu($menu);
        $data['menu']=$menu;
	
	 	$data['viewmulu']=base_url('public');
		$data['x_title'] = $this->config->item('site_short_name');
		$this->load->vars($data);
	}

   // 获取菜单
   protected function getMenu($items, $id = 'id', $pid = 'pid', $son = 'children')
    {
        $tree = array();
        $tmpMap = array();
        //修复父类设置islink=0，但是子类仍然显示的bug
        foreach( $items as $item ){
            if( $item['pid']==0 ){
                $father_ids[] = $item['id'];
            }
        }
        //----
        foreach ($items as $item) {
            $tmpMap[$item[$id]] = $item;
        }

        foreach ($items as $item) {
            //修复父类设置islink=0，但是子类仍然显示的bug by shaoneng
            if( $item['pid']<>0 && !in_array( $item['pid'], $father_ids )){
                continue;
            }
            //----
            if (isset($tmpMap[$item[$pid]])) {
                $tmpMap[$item[$pid]][$son][] = &$tmpMap[$item[$id]];
            } else {
                $tree[] = &$tmpMap[$item[$id]];
            }
        }
        return $tree;
    }
    /**
     * 创建售后单号
     * @return string
     */
    public function create_shop_money_no(){
        $order_number = 'SM'.date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        return $order_number;
    }
}















